/*
    ::: COLLSION OBJECT :::
    Takes player object as input
    
    Functions:
    Detect: Should be run every time the player is moved. Runs the different 
*/    
    var collision = {
        player: {},
        playerPoint: {topLeft: {x: 0, y: 0}, topRight: {x: 0, y: 0}, botLeft: {x: 0, y: 0}, botRight: {x: 0, y: 0}},
        showPlayerpoints: false,
        divsAppended: false,
        collisionType: {top: false, left: false, bottom: false, right: false},
        collisionClass: "colobject",
        collisionObject: null,
                
        //Detects collision from player position
        detect: function(player) {
            if(player) {
                //Getting the player object
                this.player = player;
            
                //Setting playerpoints information
                this.setPlayerPoints();
                
                //Chekking to see if there is a collision
                if(this.isCollision()) {
                    this.updatePlayerPoints();
                    return {collisionObject: this.collisionObject, collisionType: this.collisionType, detectedCollision: true}
                } else {
                    this.updatePlayerPoints();
                    return {detectedCollision: false}
                }
            
            } else {
                alert('No player object given in Collicion object!!!!');
                return {detectedCollision: false}
            }
        },
        
        //Seting the different cillision points on player
        setPlayerPoints: function() {
            
            //Setting point 1 in topLeft corner
            this.playerPoint.topLeft.x = player.x;
            this.playerPoint.topLeft.y = player.y;
            
            //Setting point 2 in topRight corner
            this.playerPoint.topRight.x = player.x + player.width;
            this.playerPoint.topRight.y = player.y;
            
            //Setting point 3 in botLeft corner
            this.playerPoint.botLeft.x = player.x;
            this.playerPoint.botLeft.y = player.y + player.height;
            
            //Setting point 4 in botRight corner
            this.playerPoint.botRight.x = player.x + player.width;
            this.playerPoint.botRight.y = player.y + player.height;
            
        },
        
        //Skal playerpoints blive vist?
        showPlayerPoints: function() {
            this.showPlayerpoints = true;
        },
        
        //Opdatere playerpoints på brugeren
        updatePlayerPoints: function() {
            if(this.showPlayerpoints) {
                if(!this.divsAppended) {
                    $('#map').append('<div id="collisionPoint1" class="collisionPoint"></div><div id="collisionPoint2" class="collisionPoint"></div><div id="collisionPoint3" class="collisionPoint"></div><div id="collisionPoint4" class="collisionPoint"></div>')
                    $('.collisionPoint').css({display: "block", position: "absolute", width: "2px", height: "2px", zIndex: "100"})
                    this.divsAppended = true;
                }
                
                $('#collisionPoint1').css({left: this.playerPoint.topLeft.x + 'px' ,top: this.playerPoint.topLeft.y + 'px' });
                $('#collisionPoint2').css({left: this.playerPoint.topRight.x + 'px' ,top: this.playerPoint.topRight.y + 'px' });
                $('#collisionPoint3').css({left:  this.playerPoint.botLeft.x + 'px' ,top:  this.playerPoint.botLeft.x + 'px' });
                $('#collisionPoint4').css({left:  this.playerPoint.botRight.x + 'px' ,top:  this.playerPoint.botRight.y + 'px' }); 
            }
        },
        
        //Detecting collision with other object
        isCollision: function() {
            var playerPoint = this.playerPoint;
            var collisionType = this.collisionType;
            var collisionAlert = false;
            var collsionObject = null;
            
            $('.' + this.collisionClass).each(function() {
                
                    //Collision object information
                    var objectWith = parseInt($(this).css("width"));
                    var objectHeight = parseInt($(this).css("height"));
                    var objectPosX = parseInt($(this).css("left"));
                    var objectPosY = parseInt($(this).css("top"));
        
                    //Collision object points
                    var objectPointTopLeft = {x: objectPosX, y: objectPosY};
                    var objectPointTopRight = {x: objectPosX + objectWith, y: objectPosY}
                    var objectPointBotLeft = {x: objectPosX, y: objectPosY + objectHeight}
                    var objectPointBotRight = {x: objectPosX + objectWith, y: objectPosY + objectHeight};
                    
                    //Comming from left side
                    if(playerPoint.topRight.x >= objectPointTopLeft.x && playerPoint.topLeft.x <= objectPointTopRight.x) {
                        if(playerPoint.botRight.y >= objectPointTopRight.y && playerPoint.topRight.y <= objectPointBotRight.y) {
                            collisionAlert = true;
                            collsionObject = this;
                            
                            if(playerPointBotRight.y == objectPointTopRight.y) {
                                collisionType.top = true;
                            }  else if(playerPoint.topRight.y == objectPointBotRight.y) {
                                collisionType.bottem = true;
                            } else if(playerPoint.topRight.x == objectPointTopLeft.x) {
                                collisionType.right = true; 
                            } else if(playerPoint.topLeft.x == objectPointTopRight.x) {
                                collisionType.left = true;
                            }
                        }
                    }
                
            })
            
            this.collisionType = collisionType;
            this.collisionObject = collsionObject;
            return collisionAlert;
        },
        
        //Changes the collision class
        setCollisionClass: function(className) {
            if(className.isString()) {
                this.collisionClass = className;
            }
        }        
        
    }